package com.omuao.message.queue.api.push;

import com.omuao.message.queue.api.ApiResult;
import com.omuao.message.queue.core.QueueData;
import com.omuao.message.queue.core.rocketmq.MessageChannelProcessor;
import com.omuao.message.queue.service.push.PushService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 放控制器
 *
 * @author omuao
 */
@RestController
@RequestMapping("message/push")
public class PushPrimaryKeyController {

    /**
     * 日志
     */
    public static Logger logger = LoggerFactory.getLogger(PushPrimaryKeyController.class);

    @Autowired
    PushService pushService;

    /**
     * 正常消息，排队进入RocksDb(Stream方式)
     *
     * @param message 消息
     * @return
     */
    @StreamListener(MessageChannelProcessor.DELAY_INPUT)
    public void onDelayMessage(QueueData message) {
        Long delayTime = -1L;
        if (message.getPattern() == null) {
            try {
                delayTime = message.getDelayTime() == null ? -1 : Long.parseLong(message.getDelayTime());
            } catch (Exception e) {
                logger.warn("格式有误！");
            }
            pushService.commit(message.getKey(), message.getContent(), delayTime);
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat(message.getPattern());
            Date time = null;
            try {
                time = dateFormat.parse(message.getDelayTime());
            } catch (ParseException e) {
                logger.warn("格式有误！");
            }
            delayTime = time.getTime();
            pushService.commit(message.getKey(), message.getContent(), delayTime);
        }

    }


    /**
     * 正常消息，排队出去
     *
     * @param message 消息
     * @return
     */
    @RequestMapping("normal")
    public ApiResult normal(@RequestParam("id") String id, @RequestParam("message") String message) {
        pushService.commit(id, message);
        return ApiResult.success();
    }

    /**
     * 进入延迟队列，等待时间正常发送出去
     *
     * @param message   消息
     * @param timestamp 时间戳
     * @return
     */
    @RequestMapping("delay")
    public ApiResult delay(@RequestParam("id") String id, @RequestParam("message") String message, @RequestParam("timestamp") Long timestamp) {
        pushService.commit(id, message, timestamp);
        return ApiResult.success();
    }

    /**
     * 进入延迟队列，等待时间正常发送出去
     *
     * @param message 消息
     * @param date    时间戳
     * @param pattern 时间格式
     * @return
     */
    @RequestMapping("delay/pattern")
    public ApiResult delayPattern(@RequestParam("id") String id, @RequestParam("message") String message, @RequestParam("date") String date, @RequestParam("pattern") String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        Date time = null;
        try {
            time = dateFormat.parse(date);
        } catch (ParseException e) {
            logger.warn("格式有误！");
        }
        if (time != null) {
            pushService.commit(id, message, time.getTime());
        }
        return ApiResult.success();
    }

    /**
     * 模板消息
     *
     * @param message    消息
     * @param parameters 参数
     * @return
     */
    @RequestMapping("template")
    public ApiResult template(@RequestParam("id") String id, @RequestParam("message") String message, @RequestParam("params") Map<String, Object> parameters) {
        return ApiResult.success();
    }


}
